package Thread_.homework;

import java.util.concurrent.Semaphore;

/**
 * 编写代码实现两个线程 增加同一个变量
 * 使用 Semphore 来控制线程安全.
 */
public class test20 {
    private static int count=0;
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(1);
        Thread t1=new Thread(()->{
            for(int i=0;i<1000;i++){
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                count++;
                semaphore.release();
            }
        });
        Thread t2=new Thread(()->{
            for(int i=0;i<1000;i++){
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                count++;
                semaphore.release();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count= "+count);
    }
}

